package com.gin.web.session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * session 任务，扫描过期的 session
 */
public class SessionTask implements Runnable {

    private Logger logger = LoggerFactory.getLogger(SessionTask.class);
    /**
     * 多少毫秒扫描一次
     * 3 分钟
     */
    private static final int SLEEP_TIME = 3 * 60 * 1000;

    /**
     * 过期时间是多少毫秒
     */
    private long millis;

    public SessionTask(long millis) {
        this.millis = millis;
    }

    /**
     * 执行任务
     */
    @Override
    public void run() {
        Thread.currentThread().setName(SessionTask.class.getName());
        while (true) {
            try {
                Thread.sleep(SLEEP_TIME);
                logger.debug("start clear overdue session.");
                SessionPool.clearOverdueSession(millis);
                logger.debug("clear overdue session end.");
            } catch (Exception e) {
                logger.error("clear overdue session error", e);
            }
        }
    }
}
